﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace Modbus_Inspector
{

    public partial class ModbusInvestigatorForm1 : Form
    {
        TcpClient tcpAsyClient;

        public ModbusInvestigatorForm1()
        {
            InitializeComponent();
        }

        public void connect(string ip, int port)
        {
            try
            {
                // ----------------------------------------------------------------
                // Connect asynchronous client
                tcpAsyClient = new TcpClient(ip, port);
                tcpAsyClient.ReceiveBufferSize = 256;
                tcpAsyClient.SendBufferSize = 256;
                label1.Text = "Connected";
                label1.ForeColor = Color.Green ; 
            }
            catch
            {
                MessageBox.Show("Could not connect to port 502", "Modbus/TCP Investigator");
            }
        }
        
        
        private void button1_Click(object sender, EventArgs e)
        {
            connect(textBox1.Text, 502);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void ModbusInvestigatorForm1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void aboutModbusTesterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show( "Version 0.1\nRelease 25 May 2008\n(c)SmartGreenBuildings", "About Modbus Inspector");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[1024];
            int bytesread, optr;

            // make sure connection actually exists
            if (tcpAsyClient == null)
            {
                MessageBox.Show("Establish connection first", "Modbus/TCP Inspector");
                return;
            }


            // make sure our connection is still available
            if (!tcpAsyClient.Connected)
            {
                MessageBox.Show("Connection not established", "Modbus/TCP Inspector");
                return;
            }


            // build the read register message

            // Modbus/TCP Application Protocol header (MBAP Header)

            data[0] = 0;            // Transaction number 
            data[1] = 0;
            data[2] = 0;            // Modbus protocol
            data[3] = 0;
            data[4] = 0;            // number of following bytes
            data[5] = 6;
            data[6] = 1;            // Unit ID of remote slave on serial connection

            // Modbus message payload

            data[7] = 0x03;        // read holding registers
            data[8] = 0;           // start address h
            data[9] = 0;           // start address l
            data[10] = 0;          // length h 
            data[11] = 125;        // length l

            optr = 12;

            tcpAsyClient.GetStream().Write( data, 0, optr );
            
            bytesread = tcpAsyClient.GetStream().Read(data, 0, 20);

            if ( bytesread > 0 )
            {
                string outp = "" ;

                for (int i = 0; i < Math.Max(5,bytesread); i++)
                {
                    outp += data[i] + " " ;
                }
                MessageBox.Show(outp) ;
			}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            connect("127.0.0.1", 502);
        }

    }
}
